import requests
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

# Create your views here.


class Codes(APIView):

    def get(self, request):
        codes = request.GET.get('code')
        print(codes)

        data = {
            "client_id": "3331683916",
            "client_secret": "b61b38d63070eac6bf8f717f6701fa09",
            "grant_type": "authorization_code",
            "code": codes,
            "redirect_uri": "http://127.0.0.1:8000/md_admin/weibo",
        }
        r = requests.post("https://api.weibo.com/oauth2/access_token", data)
        access_token = r.json()['access_token']
        uid = r.json()['uid']
        print(access_token, uid)

        return Response({'code': 200, 'msg': '微博登录成功'})


# def co(request):
#     code = request.GET.get('code')
#     print('%s', code)
#
#     data = {
#         "client_id": "3331683916",
#         "client_secret": "b61b38d63070eac6bf8f717f6701fa09",
#         "grant_type": "authorization_code",
#         "code": code,
#         "redirect_uri": "http://127.0.0.1:8000/md_admin/weibo",
#     }
#     r = requests.post("https://api.weibo.com/oauth2/access_token", data)
#     print(r.json())
#     return HttpResponse('微博登录成功')